<template>
  <div class="">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-if="editFlag"
                   v-hasPermi="['develop:FactoryMaterialAssist:add']">新增辅材检验
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="dataList" @selection-change="handleSelectionChange" ref="table"
              :max-height="tableMaxHeight">
      <el-table-column type="selection" width="55" align="center"/>
<!--      <el-table-column label="辅材类型" align="center" prop="type"/>-->
      <el-table-column label="来料检验批次号" align="center" prop="batchNumber"/>
      <el-table-column label="样品编号" align="center" prop="sampleNumber"/>
      <el-table-column label="原材料制造商" align="center" prop="producer"/>
      <el-table-column label="检测结果" align="center" prop="detectionResult"/>
      <el-table-column label="锌层厚度" align="center" prop="znThick"/>
      <el-table-column label="出厂检测日期" align="center" prop="leaveInspectDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.leaveInspectDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="入厂检测日期" align="center" prop="arriveInspectDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.arriveInspectDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="fileList" label="生产厂检测报告" align="center">
        <template slot-scope="scope">
          <file-upload-show :value="scope.row.materialReport"></file-upload-show>
        </template>
      </el-table-column>
      <el-table-column prop="fileList" label="来料检测报告" align="center">
        <template slot-scope="scope">
          <file-upload-show :value="scope.row.productReport"></file-upload-show>
        </template>
      </el-table-column>
      <el-table-column label="是否合格" align="center" prop="invoice">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.yes_no" :value="scope.row.testPass"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="180">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-document"
                     @click="handleDetail(scope.row)">详情
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" v-if="editFlag"
                     @click="handleUpdate(scope.row)" v-hasPermi="['develop:FactoryMaterialAssist:edit']">修改
          </el-button>
          <el-popconfirm :title="`确认删除当前数据项？`" @confirm="handleDelete(scope.row)" v-if="editFlag"
                         style="margin-left: 10px;">
            <el-button slot="reference" size="mini" type="text" icon="el-icon-delete"
                       v-hasPermi="['develop:FactoryMaterialAssist:remove']">删除
            </el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize" @pagination="getList"/>

    <factoryMaterialAssist-form ref="form" @ok="getList"></factoryMaterialAssist-form>
    <factory-material-assist-detail ref="detail" @ok="getList"></factory-material-assist-detail>
  </div>
</template>

<script>
import {deleteAction, getAction} from '@/api/manage'
import FactoryMaterialAssistForm from '@/views/develop/factory/factoryMaterialAssist/module/FactoryMaterialAssistForm'
import FileUploadShow from "@/components/FileUpload/FileUploadShow.vue";
import FactoryMaterialAssistDetail
  from "@/views/develop/factory/factoryMaterialAssist/module/FactoryMaterialAssistDetail.vue";

export default {
  name: "FactoryMaterialAssistList",
  dicts: ['yes_no'],
  components: {
    FactoryMaterialAssistDetail,
    FileUploadShow,
    FactoryMaterialAssistForm,
  },
  props: {
    workOrderId: {
      type: String,
      default: null
    },
    goodsId: {
      type: String,
      default: null
    },
    goodsSaleId: {
      type: String,
      default: null
    },
    daohuoDetailId: {
      type: String,
      default: null
    },
    rukuId: {
      type: String,
      default: null
    },
    produceRecordDetailId: {
      type: String,
      default: null
    },
    sampleId: {
      type: String,
      default: null
    },
    editFlag:{
      type:Boolean,
      default:false
    },
  },
  watch: {
    workOrderId: {
      handler(newVal, oldVal) {
        this.queryParams.workOrderId = newVal;
        if(newVal){
          this.getList();
        }else{
          this.dataList = [];
          this.loading = false;
        }
      },
      immediate: true
    },
    daohuoDetailId: {
      handler(newVal, oldVal) {
        this.queryParams.daohuoDetailId = newVal;
        if(newVal){
          this.getList();
        }else{
          this.dataList = [];
          this.loading = false;
        }
      },
      immediate: true
    },
    rukuId: {
      handler(newVal, oldVal) {
        this.queryParams.rukuId = newVal;
        if(newVal){
          this.getList();
        }else{
          this.dataList = [];
          this.loading = false;
        }
      },
      immediate: true
    },
    produceRecordDetailId: {
      handler(newVal, oldVal) {
        this.queryParams.produceRecordDetailId = newVal;
        if(newVal){
          this.getList();
        }
      },
      immediate: true
    },
    sampleId: {
      handler(newVal, oldVal) {
        this.queryParams.sampleId = newVal;
        if(newVal){
          this.getList();
        }
      },
      immediate: true
    },
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 原材料（辅材）表格数据
      dataList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        workOrderId: null,
        type: null,
        batchNumber: null,
        producer: null,
        sampleNumber: null,
        detectionResult: null,
        znThick: null,
        leaveInspectDate: null,
        arriveInspectDate: null,
        qualityCertificate: null,
      },
      url: {
        list: '/factoryMaterialAssist/factoryMaterialAssist/list',
        remove: '/factoryMaterialAssist/factoryMaterialAssist/',
        export: '/factoryMaterialAssist/factoryMaterialAssist/export',
      },
      tableMaxHeight: window.innerHeight - 320,
    };
  },
  created() {
    window.addEventListener('resize', this.getTableHeight);
  },
  destroyed() {
    window.removeEventListener('resize', this.getTableHeight)
  },
  methods: {
    /** 查询原材料（辅材）列表 */
    getList() {
      this.loading = true;
      getAction(this.url.list, this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getTableHeight() {
      this.tableHeight = window.innerHeight - 320
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      let workOrderId = this.workOrderId;
      let goodsId = this.goodsId;
      let goodsSaleId = this.goodsSaleId;
      let daohuoDetailId = this.daohuoDetailId;
      let produceRecordDetailId = this.produceRecordDetailId;
      let sampleId = this.sampleId;
      if (workOrderId) {
        this.$refs.form.handleAddByWorkOrderId(workOrderId,goodsId,goodsSaleId);
      }

      if (produceRecordDetailId) {
        this.$refs.form.handleAddByProduceDetailId(produceRecordDetailId,goodsId,goodsSaleId);
        return
      }

      if (daohuoDetailId) {
        this.$refs.form.handleAddByDaohuoDetailId(daohuoDetailId,goodsId);
      }

      if (sampleId) {
        this.$refs.form.handleAddBySampleId(sampleId,goodsId,goodsSaleId);
      }

    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.form.handleUpdate(row);
    },

    /** 详情按钮操作 */
    handleDetail(row) {
      this.$refs.detail.showDetail(row);
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      deleteAction(this.url.remove, ids).then(response => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(this.url.export, {
        ...this.queryParams
      }, `factoryMaterialAssist_${new Date().getTime()}.xlsx`)
    },
  }
};
</script>
